package com.softeem.score.manager.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.softeem.score.common.exception.MyException;
import com.softeem.score.common.log.annotation.Log;
import com.softeem.score.common.log.enums.OperatorType;
import com.softeem.score.manager.listener.ExcelListener;
import com.softeem.score.manager.service.StudentService;
import com.softeem.score.model.dto.StudentDto;
import com.softeem.score.model.entity.school.Student;
import com.softeem.score.model.vo.common.Result;
import com.softeem.score.model.vo.common.ResultCodeEnum;
import com.softeem.score.model.vo.execlVo.StudentExcelVo;
import com.softeem.score.model.vo.school.StudentVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2024-02-14
 */
@Tag(name = "学生管理")
@RestController
@RequestMapping("/admin/school/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @Log(title = "学生模块", businessType = 1, operatorType = OperatorType.TEACHER)
    @Operation(summary = "Excel批量导入学生数据")
    @PostMapping("importData")
    public Result importData(@RequestParam("file") MultipartFile file, @RequestParam("classesId") Long classesId) {
        try {
            // 创建监听器对象，传递mapper对象和classesId
            ExcelListener<StudentExcelVo> excelListener = new ExcelListener<>(studentService, classesId);
            // 调用read方法读取excel数据
            EasyExcel.read(file.getInputStream(), StudentExcelVo.class, excelListener).sheet(0).doRead();
            return Result.build(null, ResultCodeEnum.SUCCESS);
        } catch (IOException e) {
            throw new MyException(ResultCodeEnum.DATA_ERROR);
        }
    }

    @Operation(summary = "分页查询学生列表")
    @GetMapping("/findByPage/{pageNum}/{pageSize}")
    public Result<PageInfo<StudentVo>> findByPage(StudentDto studentDto,
                                                  @PathVariable(value = "pageNum") Integer pageNum,
                                                  @PathVariable(value = "pageSize") Integer pageSize) {
        PageInfo<StudentVo> pageInfo = studentService.findByPage(studentDto, pageNum, pageSize);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "学生模块", businessType = 1, operatorType = OperatorType.TEACHER)
    @Operation(summary = "保存学生信息")
    @PostMapping(value = "/saveStudent")
    public Result saveSysUser(@RequestBody Student student) {
        studentService.saveStudent(student);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "学生模块", businessType = 2, operatorType = OperatorType.TEACHER)
    @Operation(summary = "更新学生信息")
    @PutMapping(value = "/updateStudent")
    public Result updateSysUser(@RequestBody Student student) {
        System.out.println("student = " + student);
        studentService.updateStudent(student);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "学生模块", businessType = 3, operatorType = OperatorType.MANAGE)
    @Operation(summary = "通过学号删除学生")
    @DeleteMapping(value = "/deleteStudentByStudentNumber/{studentNumber}")
    public Result deleteById(@PathVariable(value = "studentNumber") String studentNumber) {
        studentService.deleteByStudentNumber(studentNumber);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Log(title = "批量删除学生", businessType = 3, operatorType = OperatorType.MANAGE)
    @Operation(summary = "批量删除学生")
    @DeleteMapping(value = "/deleteStudents")
    public Result deleteStudents(@RequestParam(name = "studentIds", required = false) List<Long> studentIds) {
        studentService.deleteStudents(studentIds);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }
}

